<?php

/**
 * Service class for acl module
 *
 * @author Ishtiaque Shahrier <md.ishtiaque@newgenbd.net>
 * @package acl
 * @subpackage libraries
 */
class service_l_acl {

    public static function get_user_group($user_m_user_or_id) {
        $user_id = get_id($user_m_user_or_id);
        $obj = new group_user_m_acl();
        $obj->get_by_user_id($user_id);
        if ($obj->exists()) {
            return $obj->get_group();
        } else {
            return NULL;
        }
    }

    public static function get_group_permissions($group_id, $get_simple_data=FALSE) {
        $obj = new group_permission_m_acl();
        $associations = $obj->get_by_acl_group_id($group_id);
        $permissions = array();
        foreach ($associations as $a) {
            if (($tmp = $a->get_permission_object()) !== NULL) {
                if ($get_simple_data === TRUE) {
                    $permissions[] = $tmp->convert_to_simple_data();
                } else {
                    $permissions[] = $tmp;
                }
            }
        }
        return $permissions;
    }

    public function list_all_groups($to_simple_data=FALSE) {
        $groups = new group_m_acl();
        $groups->order_by("name", "ASC")->get();
        $return = array();
        if ($groups->result_count() > 0) {
            if ($to_simple_data) {
                foreach ($groups as $group) {
                    $return[] = $group->convert_to_simple_data();
                }
            } else {
                $return = $groups;
            }
        }
        return $return;
    }

    public static function run_permission_driver(a_model2data_converter $permission_data, $user_id, $group_id, $param=NULL) {
        if ($permission_data->driver === module_u_acl::DRIVER_ALLOWED) {
            return TRUE;
        } elseif ($permission_data->driver === module_u_acl::DRIVER_NOT_ALLOWED) {
            return FALSE;
        } else {
            $driver_class = $permission_data->driver;
            $file_name = get_module_path("acl", module_u_acl::ACL_PERMISSION_DRIVERS_FOLDER) . $driver_class . EXT;
            if (file_exists($file_name)) {
                require_once $file_name;
                $permission_driver = new $driver_class($permission_data, $user_id, $group_id);
                return $permission_driver->run($param);
            } else {
                logger::i()->warning("Driver [$driver_class] not found!!Permission denied!", "driver", "acl");
            }
        }
    }

    public function create_group($name, $desc="") {
        $group = new group_m_acl();
        $group->name = $name;
        $group->description = $desc;
        return $group->save()  ? $group->id : NULL;
    }

    public function delete_group($group_id){
        $group = new group_m_acl();
        return $group->get_by_id($group_id)->delete() ? $group_id : NULL;
    }

    public function get_sys_menu_items($user_id_or_object){        
        $acl_group = $this->get_user_group($user_id_or_object);
        $items = NULL;
        $menu_item = new admin_menu_item_m_acl();
        if($acl_group===NULL){
            $items = $menu_item->get_items();
        }else{            
            $items = $menu_item->get_items($acl_group->id);
        }

        return count($items)>0 ? $items : NULL;
        
    }

}

?>